﻿namespace DesignModeConsole.结构型.组合模式;

/// <summary>
/// 抽离出来文件系统抽象类
/// </summary>
internal abstract class FileSystemModeRefactor
{
    private readonly List<FileSystemMode> _subNodes = new List<FileSystemMode>();

    public FileSystemModeRefactor(string path)
    {
        Path = path;
    }

    public string Path { get; set; }

    public bool IsFile { get; set; }

    public abstract int CountNumOfFiles();

    public abstract long CountSizeOfFile();
}

internal class FileExtension : FileSystemModeRefactor
{
    public FileExtension(string path) : base(path)
    {
    }

    public override int CountNumOfFiles()
    {
        return 1;
    }

    public override long CountSizeOfFile()
    {
        var exist = File.Exists(Path);
        if (!exist)
            return 0;

        return File.ReadAllBytes(Path).Length;
    }
}

internal class DirectoryExtension : FileSystemModeRefactor
{
    private List<FileSystemModeRefactor> SubNodes = new List<FileSystemModeRefactor>();

    public DirectoryExtension(string path) : base(path)
    {
    }

    public override int CountNumOfFiles()
    {
        var numOfFile = 0;
        foreach (var item in SubNodes)
        {
            numOfFile += item.CountNumOfFiles();
        }
        return numOfFile;
    }

    public override long CountSizeOfFile()
    {
        long sizeOfFile = 0;
        foreach (var item in SubNodes)
        {
            sizeOfFile += item.CountSizeOfFile();
        }
        return sizeOfFile;
    }

    public void AddSubNode(FileSystemModeRefactor fileSystem)
    {
        SubNodes.Add(fileSystem);
    }

    public void Remove(FileSystemModeRefactor fileSystem)
    {
        SubNodes.Remove(fileSystem);
    }
}